package com.wellsoft.mobileoa.utils.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Data;
import android.util.Log;

import com.wellsoft.mobileoa.utils.BaseAction;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.BaseView;
import com.wellsoft.mobileoa.utils.SystemConfig;
import com.wellsoft.mobileoa.wsmodel.WSOA_Action;
import com.wellsoft.mobileoa.wsmodel.WSOA_Address;
import com.wellsoft.mobileoa.wsmodel.WSOA_AddressProcess_Request;
import com.wellsoft.mobileoa.wsmodel.WSOA_AddressProcess_Response;
import com.wellsoft.mobileoa.wsmodel.WSOA_PostField;
import com.wmobile.utils.BaseObject;
import com.wmobile.utils.PopConfirmDialog;
import com.wmobile.utils.ServiceResponse;

public class SyncAddress extends BaseAction {
	List<Address> addressBooks = new ArrayList<Address>();

	public SyncAddress(WSOA_Action action, BaseView baseView) {
		this.action = action;
		this.baseView = baseView;
	}

	@Override
	public ServiceResponse excuteAction() {
		if(!isMainThread){
			if (action.getPageConfirm() != null
					&& !SystemConfig.stringIsNullOrEmpty(action.getPageConfirm()
							.getMessage())) {
				PopConfirmDialog confirmDialog = new PopConfirmDialog(baseView.getBaseActivity(),
						action.getPageConfirm().getMessage());
				if (confirmDialog.showDialog() != PopConfirmDialog.DialogResultOK) {
					return new ServiceResponse(false);
				}
			}
		}
		List<String> funcType = getFuncType(action.getFuncType());
		List<WSOA_Address> wsAddresses = getAddress();

		WSOA_AddressProcess_Request request = new WSOA_AddressProcess_Request();
		request.setProcessType(funcType.get(3));
		request.setDownloadUrl(funcType.get(1));
		request.setUploadUrl(funcType.get(2));
		request.setUser(SystemConfig.getUserName());
		request.setAddress(wsAddresses);
		request.setCookies(SystemConfig.getCookies());

		Map<String, BaseObject> httpRequests = new HashMap<String, BaseObject>();

		httpRequests.put("request", request);
		ServiceResponse httpGetResponse = null;
		if (isMainThread) {
			httpGetResponse = SystemConfig.getService()
					.CallserviceOnMainThread(httpRequests,
							"WSOA_AddressProcess",
							WSOA_AddressProcess_Response.class);
		} else {
			httpGetResponse = SystemConfig.getService().Callservice(
					httpRequests, "WSOA_AddressProcess",
					WSOA_AddressProcess_Response.class);
		}
		if (!httpGetResponse.getExeFlag()) {
			return httpGetResponse;
		}
		WSOA_AddressProcess_Response response = (WSOA_AddressProcess_Response) httpGetResponse
				.getResponseData();
		if (response.getExeFlag() == 2) {
			if (SystemConfig.reLogin()) {
				return excuteAction();
			} else {
				BaseActivity.returnLoginActivity();
				return new ServiceResponse(false);
			}
		}
		if (response.getExeFlag() == 0) {
//			baseView.getBaseActivity().showMessage(response.getMessage());
			return new ServiceResponse(false, response.getMessage());
		}

		if (request.getProcessType().equals("down")
				|| request.getProcessType().equals("updown")) {
			if (response.getAddress() != null) {
				try {
					setAddress(response.getAddress());
				} catch (RemoteException e) {
					e.printStackTrace();
				} catch (OperationApplicationException e) {
					e.printStackTrace();
				}
			}
		}

		if(!isMainThread){
			baseView.getBaseActivity().showMessage("同步成功");
		}
		return new ServiceResponse(true, "同步成功");
	}

	public List<WSOA_Address> getAddress() {
		List<WSOA_Address> wsAddresses = new ArrayList<WSOA_Address>();

		Uri uri = Uri.parse("content://com.android.contacts/contacts");
		ContentResolver resolver = baseView.getBaseActivity()
				.getContentResolver();
		Cursor cursor = resolver.query(uri, new String[] { "_id" }, null, null,
				null);
		while (cursor.moveToNext()) {
			Long contactID = cursor.getLong(0);
			WSOA_Address wsaddress = new WSOA_Address();
			Address address = new Address();
			address.setRecordId(contactID);
			List<WSOA_PostField> postFields = new ArrayList<WSOA_PostField>();

			uri = Uri.parse("content://com.android.contacts/contacts/"
					+ contactID + "/data");
			Cursor cursor1 = resolver.query(uri, new String[] { "mimetype",
					Phone.TYPE, "data1", "data2", "data3", "data4" }, null,
					null, null);
			while (cursor1.moveToNext()) {
				String data1 = cursor1.getString(cursor1
						.getColumnIndex("data1"));
				String data3 = cursor1.getString(cursor1
						.getColumnIndex("data3"));
				String data4 = cursor1.getString(cursor1
						.getColumnIndex("data4"));
				Integer type = 0;
				try {
					type = Integer.parseInt(cursor1.getString(cursor1
							.getColumnIndex("data2")));
				} catch (Exception e) {
					Log.v("syncaddress", e.getMessage());
				}
				String mimeType = cursor1.getString(cursor1
						.getColumnIndex("mimetype"));
				if ("vnd.android.cursor.item/name".equals(mimeType)) { // 是姓名
					WSOA_PostField field = new WSOA_PostField();
					field.setFieldName("name");
					List<String> values = new ArrayList<String>();
					values.add(data1);
					field.setFieldValues(values);
					postFields.add(field);

					address.setName(data1);
				} else if ("vnd.android.cursor.item/email_v2".equals(mimeType)) { // 邮箱
					if (type == Phone.TYPE_MOBILE) {
						WSOA_PostField field = new WSOA_PostField();
						field.setFieldName("email");
						List<String> values = new ArrayList<String>();
						values.add(data1);
						field.setFieldValues(values);
						postFields.add(field);

						address.setEmail(data1);
					}
				} else if ("vnd.android.cursor.item/phone_v2".equals(mimeType)) { // 手机
					String fieldName = "";
					if (SystemConfig.stringIsNullOrEmpty(data1)) {
						continue;
					}
					data1 = data1.replace("-", "").replace("+", "")
							.replace("(", "").replace(")", "").replace("*", "")
							.replace("#", "").replace(".", "");
					if (type == Phone.TYPE_MOBILE) {
						fieldName = "mp";
						address.setMp(data1);
					} else if (type == Phone.TYPE_HOME) {
						fieldName = "homephone";
						address.setHomephone(data1);
					} else if (type == Phone.TYPE_WORK) {
						fieldName = "phone";
						address.setPhone(data1);
					} else if (type == Phone.TYPE_FAX_WORK) {
						fieldName = "fax";
						address.setFax(data1);
					} else if (type == Phone.TYPE_FAX_HOME) {
						fieldName = "homefax";
						address.setHomefax(data1);
					} else if (type == Phone.TYPE_WORK_MOBILE) {
						fieldName = "phone2";
						address.setPhone2(data1);
					}
					WSOA_PostField field = new WSOA_PostField();
					field.setFieldName(fieldName);
					List<String> values = new ArrayList<String>();
					values.add(data1);
					field.setFieldValues(values);
					postFields.add(field);
				} else if ("vnd.android.cursor.item/postal-address_v2"
						.equals(mimeType)) { // 地址
					String fieldName = "";
					if (type == Phone.TYPE_HOME) {
						fieldName = "homeaddress";
						address.setHomeaddress(data1);
					} else if (type == Phone.TYPE_MOBILE) {
						fieldName = "address";
						address.setAddress(data1);
					}
					WSOA_PostField field = new WSOA_PostField();
					field.setFieldName(fieldName);
					List<String> values = new ArrayList<String>();
					values.add(data1);
					field.setFieldValues(values);
					postFields.add(field);
				} else if ("vnd.android.cursor.item/website".equals(mimeType)) { // 网址
					String fieldName = "homepage";
					WSOA_PostField field = new WSOA_PostField();
					field.setFieldName(fieldName);
					List<String> values = new ArrayList<String>();
					values.add(data1);
					field.setFieldValues(values);
					postFields.add(field);

					address.setHomepage(data1);
				} else if ("vnd.android.cursor.item/note".equals(mimeType)) { // 备注
					WSOA_PostField field = new WSOA_PostField();
					field.setFieldName("body");
					List<String> values = new ArrayList<String>();
					values.add(data1);
					field.setFieldValues(values);
					postFields.add(field);

					address.setBody(data1);
				} else if ("vnd.android.cursor.item/contact_event"
						.equals(mimeType)) {
					WSOA_PostField field = new WSOA_PostField();
					field.setFieldName("birthday");
					List<String> values = new ArrayList<String>();
					values.add(data1);
					field.setFieldValues(values);
					postFields.add(field);

					address.setBirthday(data1);
				} else if ("vnd.android.cursor.item/organization"
						.equals(mimeType)) {
					WSOA_PostField field = new WSOA_PostField();
					field.setFieldName("job");
					List<String> values = new ArrayList<String>();
					values.add(data4);
					field.setFieldValues(values);
					postFields.add(field);

					address.setJob(data4);
				}
			}
			cursor1.close();
			wsaddress.setFields(postFields);
			wsAddresses.add(wsaddress);
			addressBooks.add(address);
		}
		cursor.close();
		return wsAddresses;
	}

	public void setAddress(List<WSOA_Address> wsAddresses)
			throws RemoteException, OperationApplicationException {

		for (WSOA_Address wsAddress : wsAddresses) {
			Address address = new Address();
			boolean isNameExist = false;
			Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
			ContentResolver resolver = baseView.getBaseActivity()
					.getContentResolver();

			ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();

			for (WSOA_PostField field : wsAddress.getFields()) {
				if (SystemConfig.stringIsNullOrEmpty(field.getFieldName())
						|| field.getFieldValues() == null
						|| field.getFieldValues().size() == 0)
					continue;
				if (field.getFieldName().toLowerCase().equals("name")) {
					address.setName(field.getFieldValues().get(0));
				} else if (field.getFieldName().toLowerCase().equals("mp")) {
					address.setMp(field.getFieldValues().get(0));
				} else if (field.getFieldName().toLowerCase()
						.equals("homephone")) {
					address.setHomephone(field.getFieldValues().get(0));
				} else if (field.getFieldName().toLowerCase().equals("phone")) {
					address.setPhone(field.getFieldValues().get(0));
				} else if (field.getFieldName().toLowerCase().equals("fax")) {
					address.setFax(field.getFieldValues().get(0));
				} else if (field.getFieldName().toLowerCase().equals("homefax")) {
					address.setHomefax(field.getFieldValues().get(0));
				} else if (field.getFieldName().toLowerCase().equals("phone2")) {
					address.setPhone2(field.getFieldValues().get(0));
				} else if (field.getFieldName().toLowerCase().equals("email")) {
					address.setEmail(field.getFieldValues().get(0));
				} else if (field.getFieldName().toLowerCase()
						.equals("homepage")) {
					address.setHomepage(field.getFieldValues().get(0));
				} else if (field.getFieldName().toLowerCase().equals("address")) {
					address.setAddress(field.getFieldValues().get(0));
				} else if (field.getFieldName().toLowerCase()
						.equals("homeaddress")) {
					address.setHomeaddress(field.getFieldValues().get(0));
				} else if (field.getFieldName().toLowerCase().equals("body")) {
					address.setBody(field.getFieldValues().get(0));
				} else if (field.getFieldName().toLowerCase().equals("job")) {
					address.setJob(field.getFieldValues().get(0));
				} else if (field.getFieldName().toLowerCase()
						.equals("birthday")) {
					address.setBirthday(field.getFieldValues().get(0));
				}
			}
			Long recordID = isNameExist(address.getName());
			if (recordID == -1) {
				// 如果名字不存在
				ContentProviderOperation op1 = ContentProviderOperation
						.newInsert(uri).withValue("account_name", null).build();
				operations.add(op1);
				// 名字
				uri = Uri.parse("content://com.android.contacts/data");
				ContentProviderOperation op2 = ContentProviderOperation
						.newInsert(uri)
						.withValueBackReference("raw_contact_id", 0)
						.withValue("mimetype", "vnd.android.cursor.item/name")
						.withValue("data2", address.getName()).build();
				operations.add(op2);
				// 移动
				if (!SystemConfig.stringIsNullOrEmpty(address.getMp())) {
					ContentProviderOperation op3 = ContentProviderOperation
							.newInsert(uri)
							.withValueBackReference("raw_contact_id", 0)
							.withValue("mimetype",
									"vnd.android.cursor.item/phone_v2")
							.withValue("data1", address.getMp())
							.withValue("data2", Phone.TYPE_MOBILE).build();
					operations.add(op3);
				}
				// 住宅
				if (!SystemConfig.stringIsNullOrEmpty(address.getHomephone())) {
					ContentProviderOperation op4 = ContentProviderOperation
							.newInsert(uri)
							.withValueBackReference("raw_contact_id", 0)
							.withValue("mimetype",
									"vnd.android.cursor.item/phone_v2")
							.withValue("data1", address.getHomephone())
							.withValue("data2", Phone.TYPE_HOME).build();
					operations.add(op4);
				}
				// 工作
				if (!SystemConfig.stringIsNullOrEmpty(address.getPhone())) {
					ContentProviderOperation op5 = ContentProviderOperation
							.newInsert(uri)
							.withValueBackReference("raw_contact_id", 0)
							.withValue("mimetype",
									"vnd.android.cursor.item/phone_v2")
							.withValue("data1", address.getPhone())
							.withValue("data2", Phone.TYPE_WORK).build();
					operations.add(op5);
				}
				// 工作传真
				if (!SystemConfig.stringIsNullOrEmpty(address.getFax())) {
					ContentProviderOperation op6 = ContentProviderOperation
							.newInsert(uri)
							.withValueBackReference("raw_contact_id", 0)
							.withValue("mimetype",
									"vnd.android.cursor.item/phone_v2")
							.withValue("data1", address.getFax())
							.withValue("data2", Phone.TYPE_FAX_WORK).build();
					operations.add(op6);
				}
				// 住宅传真
				if (!SystemConfig.stringIsNullOrEmpty(address.getHomefax())) {
					ContentProviderOperation op7 = ContentProviderOperation
							.newInsert(uri)
							.withValueBackReference("raw_contact_id", 0)
							.withValue("mimetype",
									"vnd.android.cursor.item/phone_v2")
							.withValue("data1", address.getHomefax())
							.withValue("data2", Phone.TYPE_FAX_HOME).build();
					operations.add(op7);
				}
				// 第二电话
				if (!SystemConfig.stringIsNullOrEmpty(address.getPhone2())) {
					ContentProviderOperation op8 = ContentProviderOperation
							.newInsert(uri)
							.withValueBackReference("raw_contact_id", 0)
							.withValue("mimetype",
									"vnd.android.cursor.item/phone_v2")
							.withValue("data1", address.getPhone2())
							.withValue("data2", Phone.TYPE_WORK_MOBILE).build();
					operations.add(op8);
				}
				// 邮件
				if (!SystemConfig.stringIsNullOrEmpty(address.getEmail())) {
					ContentProviderOperation op9 = ContentProviderOperation
							.newInsert(uri)
							.withValueBackReference("raw_contact_id", 0)
							.withValue("mimetype",
									"vnd.android.cursor.item/email_v2")
							.withValue("data1", address.getEmail())
							.withValue("data2", Phone.TYPE_MOBILE).build();
					operations.add(op9);
				}
				// 网站
				if (!SystemConfig.stringIsNullOrEmpty(address.getHomepage())) {
					ContentProviderOperation op10 = ContentProviderOperation
							.newInsert(uri)
							.withValueBackReference("raw_contact_id", 0)
							.withValue("mimetype",
									"vnd.android.cursor.item/website")
							.withValue("data1", address.getHomepage())
							.withValue("data2", "2").build();
					operations.add(op10);
				}
				// 工作地址
				if (!SystemConfig.stringIsNullOrEmpty(address.getAddress())) {
					ContentProviderOperation op11 = ContentProviderOperation
							.newInsert(uri)
							.withValueBackReference("raw_contact_id", 0)
							.withValue("mimetype",
									"vnd.android.cursor.item/postal-address_v2")
							.withValue("data1", address.getAddress())
							.withValue("data2", Phone.TYPE_MOBILE).build();
					operations.add(op11);
				}
				// 住宅地址
				if (!SystemConfig.stringIsNullOrEmpty(address.getHomeaddress())) {
					ContentProviderOperation op12 = ContentProviderOperation
							.newInsert(uri)
							.withValueBackReference("raw_contact_id", 0)
							.withValue("mimetype",
									"vnd.android.cursor.item/postal-address_v2")
							.withValue("data1", address.getHomeaddress())
							.withValue("data2", Phone.TYPE_HOME).build();
					operations.add(op12);
				}
				// 备注
				if (!SystemConfig.stringIsNullOrEmpty(address.getBody())) {
					ContentProviderOperation op13 = ContentProviderOperation
							.newInsert(uri)
							.withValueBackReference("raw_contact_id", 0)
							.withValue("mimetype",
									"vnd.android.cursor.item/note")
							.withValue("data1", address.getBody())
							.withValue("data2", "2").build();
					operations.add(op13);
				}
				// 生日
				if (!SystemConfig.stringIsNullOrEmpty(address.getBirthday())) {
					ContentProviderOperation op14 = ContentProviderOperation
							.newInsert(uri)
							.withValueBackReference("raw_contact_id", 0)
							.withValue("mimetype",
									"vnd.android.cursor.item/contact_event")
							.withValue("data1", address.getBirthday())
							.withValue("data2", "2").build();
					operations.add(op14);
				}
				// 职称
				if (!SystemConfig.stringIsNullOrEmpty(address.getJob())) {
					ContentProviderOperation op15 = ContentProviderOperation
							.newInsert(uri)
							.withValueBackReference("raw_contact_id", 0)
							.withValue("mimetype",
									"vnd.android.cursor.item/organization")
							.withValue("data4", address.getJob())
							.withValue("data2", "2").build();
					operations.add(op15);
				}
				resolver.applyBatch("com.android.contacts", operations);
			} else {
				// 如果名字存在
				// 移动
				if (!SystemConfig.stringIsNullOrEmpty(address.getMp())) {
					ContentValues values = new ContentValues();
					values.put("data1", address.getMp());
					
					baseView.getBaseActivity().getContentResolver().update(
			                ContactsContract.Data.CONTENT_URI, values,
			                Data.RAW_CONTACT_ID + "=?" + " AND "
									+ ContactsContract.Data.MIMETYPE
									+ " = ?" + " AND data2=?",
									new String[] { String.valueOf(recordID),
											"vnd.android.cursor.item/phone_v2",
											String.valueOf(Phone.TYPE_MOBILE)});
				}
				// 住宅
				if (!SystemConfig.stringIsNullOrEmpty(address.getHomephone())) {
					ContentValues values = new ContentValues();
					values.put("data1", address.getHomephone());
					
					baseView.getBaseActivity().getContentResolver().update(
			                ContactsContract.Data.CONTENT_URI, values,
			                Data.RAW_CONTACT_ID + "=?" + " AND "
									+ ContactsContract.Data.MIMETYPE
									+ " = ?" + " AND data2=?",
							new String[] { String.valueOf(recordID),
									"vnd.android.cursor.item/phone_v2",
									String.valueOf(Phone.TYPE_HOME) });
					
//					ContentProviderOperation op4 = ContentProviderOperation
//							.newUpdate(uri)
//							.withSelection(
//									Data.RAW_CONTACT_ID + "=?" + " AND "
//											+ ContactsContract.Data.MIMETYPE
//											+ " = ?" + " AND data2=?",
//									new String[] { String.valueOf(recordID),
//											"vnd.android.cursor.item/phone_v2",
//											String.valueOf(Phone.TYPE_HOME) })
//							.withValue("data1", address.getHomephone()).build();
//					operations.add(op4);
				}
				// 工作
				if (!SystemConfig.stringIsNullOrEmpty(address.getPhone())) {
					ContentValues values = new ContentValues();
					values.put("data1", address.getPhone());
					
					baseView.getBaseActivity().getContentResolver().update(
			                ContactsContract.Data.CONTENT_URI, values,
			                Data.RAW_CONTACT_ID + "=?" + " AND "
									+ ContactsContract.Data.MIMETYPE
									+ " = ?" + " AND data2=?",
							new String[] { String.valueOf(recordID),
									"vnd.android.cursor.item/phone_v2",
									String.valueOf(Phone.TYPE_WORK) });
					
//					ContentProviderOperation op5 = ContentProviderOperation
//							.newUpdate(uri)
//							.withSelection(
//									Data.RAW_CONTACT_ID + "=?" + " AND "
//											+ ContactsContract.Data.MIMETYPE
//											+ " = ?" + " AND data2=?",
//									new String[] { String.valueOf(recordID),
//											"vnd.android.cursor.item/phone_v2",
//											String.valueOf(Phone.TYPE_WORK) })
//							.withValue("data1", address.getPhone()).build();
//					operations.add(op5);
				}
				// 工作传真
				if (!SystemConfig.stringIsNullOrEmpty(address.getFax())) {
					ContentValues values = new ContentValues();
					values.put("data1", address.getFax());
					
					baseView.getBaseActivity().getContentResolver().update(
			                ContactsContract.Data.CONTENT_URI, values,
			                Data.RAW_CONTACT_ID + "=?" + " AND "
									+ ContactsContract.Data.MIMETYPE
									+ " = ?" + " AND data2=?",
							new String[] { String.valueOf(recordID),
									"vnd.android.cursor.item/phone_v2",
									String.valueOf(Phone.TYPE_FAX_WORK) });
					
//					ContentProviderOperation op6 = ContentProviderOperation
//							.newUpdate(uri)
//							.withSelection(
//									Data.RAW_CONTACT_ID + "=?" + " AND "
//											+ ContactsContract.Data.MIMETYPE
//											+ " = ?" + " AND data2=?",
//									new String[] { String.valueOf(recordID),
//											"vnd.android.cursor.item/phone_v2",
//											String.valueOf(Phone.TYPE_FAX_WORK) })
//							.withValue("data1", address.getFax()).build();
//					operations.add(op6);
				}
				// 住宅传真
				if (!SystemConfig.stringIsNullOrEmpty(address.getHomefax())) {
					ContentValues values = new ContentValues();
					values.put("data1", address.getHomefax());
					
					baseView.getBaseActivity().getContentResolver().update(
			                ContactsContract.Data.CONTENT_URI, values,
			                Data.RAW_CONTACT_ID + "=?" + " AND "
									+ ContactsContract.Data.MIMETYPE
									+ " = ?" + " AND data2=?",
							new String[] { String.valueOf(recordID),
									"vnd.android.cursor.item/phone_v2",
									String.valueOf(Phone.TYPE_FAX_HOME) });
					
//					ContentProviderOperation op7 = ContentProviderOperation
//							.newUpdate(uri)
//							.withSelection(
//									Data.RAW_CONTACT_ID + "=?" + " AND "
//											+ ContactsContract.Data.MIMETYPE
//											+ " = ?" + " AND data2=?",
//									new String[] { String.valueOf(recordID),
//											"vnd.android.cursor.item/phone_v2",
//											String.valueOf(Phone.TYPE_FAX_HOME) })
//							.withValue("data1", address.getHomefax()).build();
//					operations.add(op7);
				}
				// 第二电话
				if (!SystemConfig.stringIsNullOrEmpty(address.getPhone2())) {
					ContentValues values = new ContentValues();
					values.put("data1", address.getPhone2());
					
					baseView.getBaseActivity().getContentResolver().update(
			                ContactsContract.Data.CONTENT_URI, values,
			                Data.RAW_CONTACT_ID + "=?" + " AND "
									+ ContactsContract.Data.MIMETYPE
									+ " = ?" + " AND data2=?",
							new String[] {
									String.valueOf(recordID),
									"vnd.android.cursor.item/phone_v2",
									String.valueOf(Phone.TYPE_WORK_MOBILE) });
					
//					ContentProviderOperation op8 = ContentProviderOperation
//							.newUpdate(uri)
//							.withSelection(
//									Data.RAW_CONTACT_ID + "=?" + " AND "
//											+ ContactsContract.Data.MIMETYPE
//											+ " = ?" + " AND data2=?",
//									new String[] {
//											String.valueOf(recordID),
//											"vnd.android.cursor.item/phone_v2",
//											String.valueOf(Phone.TYPE_WORK_MOBILE) })
//							.withValue("data1", address.getPhone2()).build();
//					operations.add(op8);
				}
				// 邮件
				if (!SystemConfig.stringIsNullOrEmpty(address.getEmail())) {
					ContentValues values = new ContentValues();
					values.put("data1", address.getEmail());
					
					baseView.getBaseActivity().getContentResolver().update(
			                ContactsContract.Data.CONTENT_URI, values,
			                Data.RAW_CONTACT_ID + "=?" + " AND "
									+ ContactsContract.Data.MIMETYPE
									+ " = ?" + " AND data2=?",
							new String[] { String.valueOf(recordID),
									"vnd.android.cursor.item/email_v2",
									String.valueOf(Phone.TYPE_MOBILE) });
					
//					ContentProviderOperation op9 = ContentProviderOperation
//							.newUpdate(uri)
//							.withSelection(
//									Data.RAW_CONTACT_ID + "=?" + " AND "
//											+ ContactsContract.Data.MIMETYPE
//											+ " = ?" + " AND data2=?",
//									new String[] { String.valueOf(recordID),
//											"vnd.android.cursor.item/email_v2",
//											String.valueOf(Phone.TYPE_MOBILE) })
//							.withValue("data1", address.getEmail()).build();
//					operations.add(op9);
				}
				// 网站
				if (!SystemConfig.stringIsNullOrEmpty(address.getHomepage())) {
					ContentValues values = new ContentValues();
					values.put("data1", address.getHomepage());
					
					baseView.getBaseActivity().getContentResolver().update(
			                ContactsContract.Data.CONTENT_URI, values,
			                Data.RAW_CONTACT_ID + "=?" + " AND "
									+ ContactsContract.Data.MIMETYPE
									+ " = ?" + " AND data2=?",
							new String[] { String.valueOf(recordID),
									"vnd.android.cursor.item/website",
									String.valueOf("2") });
					
//					ContentProviderOperation op10 = ContentProviderOperation
//							.newUpdate(uri)
//							.withSelection(
//									Data.RAW_CONTACT_ID + "=?" + " AND "
//											+ ContactsContract.Data.MIMETYPE
//											+ " = ?" + " AND data2=?",
//									new String[] { String.valueOf(recordID),
//											"vnd.android.cursor.item/website",
//											String.valueOf("2") })
//							.withValue("data1", address.getHomepage()).build();
//					operations.add(op10);
				}
				// 工作地址
				if (!SystemConfig.stringIsNullOrEmpty(address.getAddress())) {
					ContentValues values = new ContentValues();
					values.put("data1", address.getAddress());
					
					baseView.getBaseActivity().getContentResolver().update(
			                ContactsContract.Data.CONTENT_URI, values,
			                Data.RAW_CONTACT_ID + "=?" + " AND "
									+ ContactsContract.Data.MIMETYPE
									+ " = ?" + " AND data2=?",
							new String[] {
									String.valueOf(recordID),
									"vnd.android.cursor.item/postal-address_v2",
									String.valueOf(Phone.TYPE_MOBILE) });
					
//					ContentProviderOperation op11 = ContentProviderOperation
//							.newUpdate(uri)
//							.withSelection(
//									Data.RAW_CONTACT_ID + "=?" + " AND "
//											+ ContactsContract.Data.MIMETYPE
//											+ " = ?" + " AND data2=?",
//									new String[] {
//											String.valueOf(recordID),
//											"vnd.android.cursor.item/postal-address_v2",
//											String.valueOf(Phone.TYPE_MOBILE) })
//							.withValue("data1", address.getAddress()).build();
//					operations.add(op11);
				}
				// 住宅地址
				if (!SystemConfig.stringIsNullOrEmpty(address.getHomeaddress())) {
					ContentValues values = new ContentValues();
					values.put("data1", address.getHomeaddress());
					
					baseView.getBaseActivity().getContentResolver().update(
			                ContactsContract.Data.CONTENT_URI, values,
			                Data.RAW_CONTACT_ID + "=?" + " AND "
									+ ContactsContract.Data.MIMETYPE
									+ " = ?" + " AND data2=?",
							new String[] {
									String.valueOf(recordID),
									"vnd.android.cursor.item/postal-address_v2",
									String.valueOf(Phone.TYPE_HOME) });
					
//					ContentProviderOperation op12 = ContentProviderOperation
//							.newUpdate(uri)
//							.withSelection(
//									Data.RAW_CONTACT_ID + "=?" + " AND "
//											+ ContactsContract.Data.MIMETYPE
//											+ " = ?" + " AND data2=?",
//									new String[] {
//											String.valueOf(recordID),
//											"vnd.android.cursor.item/postal-address_v2",
//											String.valueOf(Phone.TYPE_HOME) })
//							.withValue("data1", address.getHomeaddress())
//							.build();
//					operations.add(op12);
				}
				// 备注
				if (!SystemConfig.stringIsNullOrEmpty(address.getBody())) {
					ContentValues values = new ContentValues();
					values.put("data1", address.getBody());
					
					baseView.getBaseActivity().getContentResolver().update(
			                ContactsContract.Data.CONTENT_URI, values,
			                Data.RAW_CONTACT_ID + "=?" + " AND "
									+ ContactsContract.Data.MIMETYPE
									+ " = ?" + " AND data2=?",
							new String[] { String.valueOf(recordID),
									"vnd.android.cursor.item/note",
									String.valueOf("2") });
					
//					ContentProviderOperation op13 = ContentProviderOperation
//							.newUpdate(uri)
//							.withSelection(
//									Data.RAW_CONTACT_ID + "=?" + " AND "
//											+ ContactsContract.Data.MIMETYPE
//											+ " = ?" + " AND data2=?",
//									new String[] { String.valueOf(recordID),
//											"vnd.android.cursor.item/note",
//											String.valueOf("2") })
//							.withValue("data1", address.getBody()).build();
//					operations.add(op13);
				}
				// 生日
				if (!SystemConfig.stringIsNullOrEmpty(address.getBirthday())) {
					ContentValues values = new ContentValues();
					values.put("data1", address.getBirthday());
					
					baseView.getBaseActivity().getContentResolver().update(
			                ContactsContract.Data.CONTENT_URI, values,
			                Data.RAW_CONTACT_ID + "=?" + " AND "
									+ ContactsContract.Data.MIMETYPE
									+ " = ?" + " AND data2=?",
							new String[] { String.valueOf(recordID),
									"vnd.android.cursor.item/note",
									String.valueOf("2") });
					
//					ContentProviderOperation op14 = ContentProviderOperation
//							.newUpdate(uri)
//							.withSelection(
//									Data.RAW_CONTACT_ID + "=?" + " AND "
//											+ ContactsContract.Data.MIMETYPE
//											+ " = ?" + " AND data2=?",
//									new String[] { String.valueOf(recordID),
//											"vnd.android.cursor.item/note",
//											String.valueOf("2") })
//							.withValue("data1", address.getBirthday()).build();
//					operations.add(op14);
				}
				// 职称
				if (!SystemConfig.stringIsNullOrEmpty(address.getJob())) {
					ContentValues values = new ContentValues();
					values.put("data4", address.getJob());
					
					baseView.getBaseActivity().getContentResolver().update(
			                ContactsContract.Data.CONTENT_URI, values,
			                Data.RAW_CONTACT_ID + "=?" + " AND "
									+ ContactsContract.Data.MIMETYPE
									+ " = ?" + " AND data2=?",
							new String[] {
									String.valueOf(recordID),
									"vnd.android.cursor.item/organization",
									String.valueOf("2") });
					
//					ContentProviderOperation op15 = ContentProviderOperation
//							.newUpdate(uri)
//							.withSelection(
//									Data.RAW_CONTACT_ID + "=?" + " AND "
//											+ ContactsContract.Data.MIMETYPE
//											+ " = ?" + " AND data2=?",
//									new String[] {
//											String.valueOf(recordID),
//											"vnd.android.cursor.item/organization",
//											String.valueOf("2") })
//							.withValue("data4", address.getJob()).build();
//					operations.add(op15);
				}
			}
		}
	}

	public Long isNameExist(String name) {
		if (SystemConfig.stringIsNullOrEmpty(name))
			return -1l;
		for (int i = 0; i < addressBooks.size(); i++) {
			if (addressBooks.get(i).getName().equals(name)) {
				return addressBooks.get(i).getRecordId();
			}
		}
		return -1l;
	}

	public List<String> getFuncType(String funcType) {
		String[] types = funcType.split("\"");
		List<String> resultType = new ArrayList<String>();
		for (int i = 1; i < types.length; i += 2) {
			resultType.add(types[i]);
		}
		return resultType;
	}
}
